package com.wxtwxt.ruralmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wxtwxt.ruralmall.common.utils.R;
import com.wxtwxt.ruralmall.product.entity.AttrEntity;
import com.wxtwxt.ruralmall.product.service.AttrAttrgroupRelationService;
import com.wxtwxt.ruralmall.product.service.AttrService;
import com.wxtwxt.ruralmall.product.service.CategoryService;
import com.wxtwxt.ruralmall.product.vo.AttrGroupRelationVo;
import com.wxtwxt.ruralmall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.web.bind.annotation.*;

import com.wxtwxt.ruralmall.product.entity.AttrGroupEntity;
import com.wxtwxt.ruralmall.product.service.AttrGroupService;
import com.wxtwxt.ruralmall.common.utils.PageUtils;

import javax.annotation.Resource;



/**
 * 属性分组
 *
 * @author Wxt
 * @email wxt23787@126.com
 * @date 2023-01-30 19:53:31
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos =  attrService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data", attrGroupWithAttrsVos);
    }

    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody AttrGroupRelationVo[] relationVos) {
        attrAttrgroupRelationService.saveAttrRelation(relationVos);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrService.getNoAttrRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] relationVos) {
        attrAttrgroupRelationService.deleteAttrRelation(relationVos);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
